package com.jianyun.jyzs.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.CrmDetialActivity;
import com.jianyun.jyzs.bean.CrmCalendarListBean;
import com.jianyun.jyzs.bean.CrmDetial;
import com.jianyun.jyzs.bean.CrmSearchBean;
import com.jianyun.jyzs.constant.SysConstant;
import com.jrmf360.rylib.common.util.TimeUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by mlli on 2017/9/1.
 */

public class CrmCalendarListAdapter extends RecyclerView.Adapter {
    private Context context;
    List<CrmCalendarListBean.ListBean> crmList;


    public CrmCalendarListAdapter(Context context, List<CrmCalendarListBean.ListBean> list) {
        this.context = context;
        this.crmList = list;
    }

    public void setList(List<CrmCalendarListBean.ListBean> list) {
        this.crmList = list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.crm_calendar_item_layout, parent, false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        MyHolder myHolder = (MyHolder) holder;
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        myHolder.tvCompamy.setText(crmList.get(position).getProjectname());
        String visittype = crmList.get(position).getVisittype();
        if ("电话沟通".equals(visittype)){
            //myHolder.imgVerit.setImageResource(context.getResources().getDrawable(R.drawable.crm_phone));
            myHolder.imgVerit.setImageDrawable(context.getResources().getDrawable(R.drawable.crm_phone));
        }
        if ("上门拜访".equals(visittype)){
            myHolder.imgVerit.setImageDrawable(context.getResources().getDrawable(R.drawable.crm_person));
        }
        myHolder.tvVertit.setText(visittype);


        myHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, CrmDetialActivity.class);
                CrmSearchBean crmSearchBean = new CrmSearchBean();
                CrmSearchBean.ListCrm bean = crmSearchBean.new ListCrm();
                int autoid = crmList.get(position).getAutoid();
                Log.i("test","crmId:"+autoid);
                bean.setAutoid(crmList.get(position).getAutoid());
                bean.setCustomername(crmList.get(position).getProjectname());
                intent.putExtra(SysConstant.msg_key_parcelable, bean);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return crmList == null ? 0 : crmList.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {

        private final TextView tvCompamy;
        private final TextView tvVertit;
        private final ImageView imgVerit;

        public MyHolder(View itemView) {
            super(itemView);
            tvCompamy = (TextView) itemView.findViewById(R.id.tv_company);
            tvVertit = (TextView) itemView.findViewById(R.id.tv_vertit);
            imgVerit = (ImageView) itemView.findViewById(R.id.img_vertit);
        }
    }

    public static int differentDaysByMillisecond(Date date1, Date date2) {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
        if (days <= 0) {
            return 0;
        } else {
            return days;
        }

    }
}
